package com.realgodo.site.web.user.friend;

import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.Struts2Utils;

import com.realgodo.site.entity.relation.Friend;
import com.realgodo.site.entity.relation.FriendCatalog;
import com.realgodo.site.entity.relation.FriendConfig;
import com.realgodo.site.entity.relation.Relation;
import com.realgodo.site.service.relation.FriendCatalogManager;
import com.realgodo.site.service.relation.FriendConfigManager;
import com.realgodo.site.service.relation.FriendManager;
import com.realgodo.site.web.user.UserDefaultController;


/**朋友管理*/
@Scope("prototype")
@Results({
	@Result(name = IndexController.TO_INDEX, type = "redirect", location = "${base}/user/friend/?catalogId=${catalogId}"),
	@Result(name = "toWaited", type = "redirect", location = "${base}/user/friend/index!waited"),
	@Result(name = "top", type = "redirect", location = "${base}/user/friend/index!top")
})
public class IndexController extends UserDefaultController {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4443912426815088365L;
	
	public static final String TO_INDEX="toIndex";
	@Autowired
	protected FriendCatalogManager friendCatalogManager;
	@Autowired
	protected FriendConfigManager friendCinfigManager;
	@Autowired
	protected FriendManager friendManager;
	
	protected Page<FriendConfig> friendConfigPage;
	protected Page<Friend> friendPage;
	
	protected List<FriendConfig> friendConfigList;
	
	protected Long catalogId;
	
	protected List<FriendCatalog> friendCatalogList;
	
	private String active;
	
	private Long[] ids;//id组
	
	private Long[] catalogIds;//分组id
	
	private int[] isTops;//是否首页
	
	private int[] isCares;//是否动态关注
	
	private String[] comments;//描述
	
	private Integer total;//朋友总数
	
	protected Integer topsCount;
	
	private Integer waitsCount;
	
	private int auditType=Friend.STATUS_AGREE;//朋友申请审核结果
	
	@Override
	public String execute() throws Exception {
		super.getParamMap().put("spaceId", super.getSpace().getId().toString());
		super.getParamMap().put("status", String.valueOf(Friend.STATUS_AGREE));
		return list();
	}
	
	/**首页顶级朋友*/
	public String top(){
		this.active="top";
		super.getParamMap().put("spaceId", super.getSpace().getId().toString());
		super.getParamMap().put("isTop", String.valueOf(FriendConfig.IS));
		list();
		this.topsCount=friendConfigPage.getTotalCount();
		return SUCCESS;
	}
	/**待审核朋友*/
	public String waited(){
		this.active="waited";
		super.getParamMap().put("status", String.valueOf(Friend.STATUS_NEW));
		super.getParamMap().put("receiverId", super.getSpace().getId().toString());
		friendPage=friendManager.find(10, super.getParamMap());
		super.setPageInfo(friendPage);
		this.waitsCount=friendPage.getTotalCount();
		return "wait_list";
	}
	public String list(){
		friendConfigPage=friendCinfigManager.find(10, super.getParamMap());
		super.setPageInfo(friendConfigPage);
		return SUCCESS;
	}
	
	/**删除朋友*/
	public String delete(){
		this.friendCinfigManager.remove(id);
		return returnPage();
	}
	
	/**保存变更*/
	public String save(){
		if(ids==null||ids.length==0){
			return returnPage();
		}
		FriendConfig friendConfig=null;
		FriendCatalog friendCatalog=null;
		for(int i=0;i<ids.length;i++){
			friendConfig=friendCinfigManager.get(ids[i]);
			friendConfig.setIsCare(isCares[i]);
			friendConfig.setIsTop(isTops[i]);
			if(!friendConfig.getCatalog().getId().equals(catalogIds[i])){
				if(!FriendCatalog.DEFAULT.equals(friendConfig.getCatalog().getId())){
					//移出组朋友数-1
					friendConfig.getCatalog().setFriendNum(friendConfig.getCatalog().getFriendNum()-1);
					friendCatalogManager.save(friendConfig.getCatalog());
				}
				friendCatalog=friendCatalogManager.get(catalogIds[i]);
				//移入组朋友数加1
				if(!FriendCatalog.DEFAULT.equals(catalogIds[i])){
					friendCatalog.setFriendNum(friendCatalog.getFriendNum()+1);
					friendCatalogManager.save(friendCatalog);
				}
				friendConfig.setCatalog(friendCatalog);
			}
			
			if(FriendConfigManager.DAFAULT_STRING_NULL_VALUE.equals(comments[i])){
				friendConfig.setComment(null);
			}else{
				friendConfig.setComment(comments[i]);
			}
			friendCinfigManager.save(friendConfig);
		}
		return returnPage();
	}
	
	private String returnPage(){
		if(active==null||"".equals(active.trim())){
			return TO_INDEX;
		}else{
			return active;
		}
	}
	
	
	/**申请交朋友*/
	public String apply(){
		if(friendManager.isFriend(getSpace().getId(), id)!=Friend.STATUS_REJECT){
			Struts2Utils.renderText(Boolean.FALSE.toString());
			return null;
		}
		Friend friend=new Friend();
		friend.setSender(super.getSpace());
		friend.setReceiver(super.spaceManager.get(id));
		friend.setStatus(Relation.STATUS_NEW);
		this.friendManager.save(friend);
		Struts2Utils.renderText(Boolean.TRUE.toString());
		return null;
	}
	
	/**审核交朋友请求*/
	public String audit(){
		Friend friend=this.friendManager.get(id);
		if(friend.getStatus()!=Friend.STATUS_NEW||!super.getSpace().getId().equals(friend.getReceiver().getId())){
			return "error";
		}
		if(auditType==Friend.STATUS_REJECT){
			//拒绝，删除关系
			friendManager.delete(id);
			return "toWaited";
		}
		
		friend.setStatus(this.auditType);
		this.friendManager.save(friend);
		//生成默认朋友设置
		
		FriendCatalog defaultCatalog=friendCatalogManager.get(FriendCatalog.DEFAULT);
		FriendConfig  senderConfig=new  FriendConfig();
		senderConfig.setCatalog(defaultCatalog);
		senderConfig.setConfigDate(new Date());
		senderConfig.setFriend(friend);
		senderConfig.setOnwer(friend.getSender());
		friendCinfigManager.save(senderConfig);
		
		FriendConfig  receiverConfig=new  FriendConfig();
		receiverConfig.setCatalog(defaultCatalog);
		receiverConfig.setConfigDate(new Date());
		receiverConfig.setFriend(friend);
		receiverConfig.setOnwer(friend.getReceiver());
		friendCinfigManager.save(receiverConfig);
		return TO_INDEX;
	}
	
	
	
	//----------get/set-------------//

	public Long getCatalogId() {
		return catalogId;
	}

	public void setCatalogId(Long catalogId) {
		this.catalogId = catalogId;
	}


	public Page<FriendConfig> getFriendConfigPage() {
		return friendConfigPage;
	}


	public List<FriendCatalog> getFriendCatalogList() {
		if(friendCatalogList==null){
			friendCatalogList=friendCatalogManager.getFriendCatalogList(super.getSpace().getId());
		}
		return friendCatalogList;
	}


	public int getIsManagerCatalog() {
		return 0;
	}


	public void setActive(String active) {
		this.active = active;
	}

	public String getActive() {
		return active;
	}

	public Long[] getCatalogIds() {
		return catalogIds;
	}

	public void setCatalogIds(Long[] catalogIds) {
		this.catalogIds = catalogIds;
	}

	public int[] getIsTops() {
		return isTops;
	}

	public void setIsTops(int[] isTops) {
		this.isTops = isTops;
	}

	public int[] getIsCares() {
		return isCares;
	}

	public void setIsCares(int[] isCares) {
		this.isCares = isCares;
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}

	public String[] getComments() {
		return comments;
	}

	public void setComments(String[] comments) {
		this.comments = comments;
	}
	
	public Integer getTotal(){
		if(total==null){
			total=friendManager.getFriendSpaceIdList(super.getSpace().getId()).size()-1;
			total=total<0?0:total;
		}
		return total;
	}

	public Integer getTopsCount() {
		if(topsCount==null){
			topsCount=friendCinfigManager.getTopsCount(getSpace().getId());
		}
		return topsCount;
	}

	public int getAuditType() {
		return auditType;
	}

	public void setAuditType(int auditType) {
		this.auditType = auditType;
	}

	public List<FriendConfig> getFriendConfigList() {
		return friendConfigList;
	}

	public Integer getWaitsCount() {
		return waitsCount;
	}

	public Page<Friend> getFriendPage() {
		return friendPage;
	}
	
	
}
